<template>
  <card>
    <h4 slot="header" class="card-title">管理员信息</h4>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" class="demo-ruleForm">
      <el-form-item label="姓名" required prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="工号" required prop="number">
        <el-input v-model="form.number"></el-input>
      </el-form-item>
      <el-form-item label="性别" required prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="权限" required prop="role">
        <el-select v-model="form.role" placeholder="请选择权限">
          <el-option label="超级管理员" value="1"></el-option>
          <el-option label="业主管理员" value="2"></el-option>
          <el-option label="租赁承包管理员" value="3"></el-option>
        </el-select>
      </el-form-item>

     

     
      <el-form-item>
        <el-button type="primary" @click="onSubmit">增加</el-button>
        <el-button @click="back">返回</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="请输入您的密码" :visible.sync="newDialogVisible" >
      <el-input  type="password" v-model="form.admin_password" auto-complete="off" style="width:70%;"></el-input>
    <span slot="footer" class="dialog-footer">
    <el-button @click="newDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
  </span>
    </el-dialog>
  </card>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: "",
        role: "",
        number: "",
        password:"123456",
        admin_password:"",
        sex: "",
      },
      newDialogVisible:false,
      rules: {
        username: [{ required: true, message: "请输入名字", trigger: "blur" }],
        role: [{ required: true, message: "请选择权限", trigger: "change" }],
        number: [{ required: true, message: "请输入工号", trigger: "change" }],
        sex: [{ required: true, message: "请输入性别", trigger: "change" }]
      }
    };
  },
  methods: {
    onSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.newDialogVisible = true
           
         
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submit(){
      this.newDialogVisible = false;
       this.$ajax({
            method: "post",
            url: "/admin/role",
            withCredentials: true,
            data: this.form
          })
            .then(res => {
            if(res.data.error_no===1){
              this.$message({
            type: "success",
            message: "添加成功!"
          });
            }else{
               this.$message({
            type: "info",
            message: res.data.error_msg
          });
            }
            })
            .catch(error => {
             this.$message({
            type: "info",
            message: "添加失败!"
          });
              console.log(error);
            });
    },
    back() {
      this.$router.go(-1);
    }
  }
};
</script>